package com.example.liunanxin.lnx226ordering.model;

import com.example.liunanxin.lnx226ordering.beans.UserBean;
import com.example.liunanxin.lnx226ordering.iface.DoCollectfoodListener;
import com.example.liunanxin.lnx226ordering.iface.DoCollectfoodiface;
import com.example.liunanxin.lnx226ordering.retrofitservice.CollectService;
import com.example.liunanxin.lnx226ordering.url.Url;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class DoCollectfoodModel implements DoCollectfoodiface {
    private Retrofit retrofit;

    public DoCollectfoodModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Url.USERURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void doCollect(int user_id, int food_id, final DoCollectfoodListener doCollectfoodListener){
        CollectService collectService=retrofit.create(CollectService.class);
        Call<UserBean> call=collectService.doCollectFood(user_id, food_id);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                if (response.isSuccessful()&&response!=null)
                    doCollectfoodListener.onResponse(response.body());
                else
                    doCollectfoodListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                doCollectfoodListener.onFail(t.toString());

            }
        });
    }
}
